#include "Convolution.h"

Convolution::Convolution(const short int newSize) : Matrix2D(newSize, 1)
{
    factor = 1;
}

QImage Convolution::apply(QImage * img)
{

    QImage imgAux(*img);
    int w = img->width();
    int h = img->height();
//    qDebug() << "WIDTH: " << imgAux.width();
//    qDebug() << "HEIGHT: " << imgAux.height();

    // For each pixel in the image
    for (int x = 1; x <  w - 1; x++)
        for (int y = 1; y < h - 1; y++)
        {

            float sum = 0;
            // Apply the convolution to this pixel
            for (int i = y - 1; i <= y + 1; i++)
                for (int j = x - 1; j <= x + 1; j++ )
                    // Add the pixel value times the mask value
                    sum = sum + matrix[i - y + 1][j - x + 1] * Transformations::getIntensity(img, j, i);

          //  qDebug() << "SUM sin factor : " << sum;
            sum = sum * factor;
//            qDebug() << "factor " << factor;

            //QColor color(img->pixel(x,y));
         //   qDebug() << "SUM : " << sum << "X: " << x << "Y: " << y << "ROJO: " << color.red();
            imgAux.setPixel(x, y, QColor(sum, sum, sum).rgb());
        }

    return imgAux;

}

Convolution::~Convolution()
{

}

void Convolution::setMask(Matrix2D &m)
{
    (Matrix2D)*this = m;
}
